package com.charlotte;

import java.io.*;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 *
 * @author law
 */
final public class Reboot extends Peticion
{
 
    public Reboot(String url,String passwordModem) throws IOException
    {
        super(url+
       (passwordModem != null ? (passwordModem.isEmpty() ? passwordModem : "&PASSWORD="+passwordModem) : ""));
    }
    
    @Override
    public String get()throws FileNotFoundException, IOException, UnknownHostException, SocketTimeoutException          
    {
        this.conect.connect();
        String result = null;
        try(Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(conect.getInputStream()))))
        {
            buscar:
                while(scan.hasNextLine())
                {
                    String line = scan.nextLine();
                    if(line.matches("^.*Restablecimiento.*$"))
                    {
                        result = "Modem reiniciado.";
                        break buscar;
                    }
                    else if(line.matches("^.*es\u0020incorrecta.*$"))
                    {
                        result = "Contraseña incorrecta.";
                        break buscar;
                    }
                }
        }
        this.conect.disconnect();
        return result == null ? "Error inesperado." : result;
    }
    
}